home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util4 / 0utils.lha / 0Utils / Exists.data < prev    next >
Text File  |  1995-08-19  |  3KB  |  138 lines

  1.  
  2. #ifdef TPLTER
  3.  
  4. Exists = {
  5.  
  6.     SHORT = {{ determine, if a file exists }};
  7.  
  8.     DESCRIPTION = {{
  9.     Get one filename on Commandline
  10.     and write to StdOut if it exists
  11.  
  12.     Exists gets a filename and outputs if it exists.
  13.  
  14.     The resulting string is sent to STDOUT.
  15.  
  16.     RESULT
  17.     if NAME is set the filename if the file exists,
  18.     or empty string if not else 1/0
  19.     }};
  20.  
  21.     BUGS = {{
  22.     We use 'Lock' to check the file, so an exclusively
  23.     locked file is not recognized.
  24.  
  25.     We currently do not set ERROR_FILE_NOT_FOUND if
  26.     Exists does not find a file
  27.     }};
  28.  
  29.     EXAMPLES = {{
  30.     >Exists Sys:
  31.     1
  32.  
  33.     >Exists c:copy NAME
  34.     c:copy
  35.  
  36.     >Exists jabba:the:hutt
  37.     0
  38.     }};
  39.  
  40.     TODO = {{
  41.     Use another way than 'Lock' to determine
  42.     the existance
  43.     }};
  44.  
  45.     HISTORY = {{
  46.     20-03-94 b_noll created
  47.     03-02-95 b_noll created (had forgotten the first version =8-})
  48.     11-02-95 b_noll added FULL Keyword
  49.     20-02-95 b_noll restructured source
  50.     21-02-95 b_noll added version/format-prefix/offset
  51.     20-03-95 b_noll added args diagnostics
  52.     19-08-95 b_noll created .data file
  53.     }};
  54.  
  55.  
  56.     Template = "FILE/A,NAME/S,FULL/S";
  57.  
  58.     Arguments = {{
  59.     STRPTR file;
  60.     ULONG  name;
  61.     ULONG  full;
  62.     }};
  63.  
  64.     version = "1.3";
  65.  
  66.     body = {{
  67.         APTR pw;
  68.         struct Process *p;
  69.         STRPTR ostr;
  70.         BPTR lock;
  71.  
  72.         if ((p = (struct Process *)FindTask(NULL))) {
  73.         pw = p->pr_WindowPtr;
  74.         p->pr_WindowPtr = (APTR)-1;
  75.         } /* if */
  76.  
  77.         /* ---- Existance check: rv==0->Exists, rv==5->n/e */
  78.         retval = RETURN_WARN;
  79.         {
  80. #if 1
  81.         UBYTE buffer[MAXPATHLEN];
  82.  
  83.         if ((lock = Lock(argv->file, ACCESS_READ))) {
  84.             retval = RETURN_OK;
  85.  
  86.             if (argv->full) {
  87.             NameFromLock(lock, buffer, sizeof(buffer));
  88.             ostr = buffer;
  89.             } else if (argv->name) {
  90.             ostr = argv->file;
  91.             } else {
  92.             ostr = "1";
  93.             } /* if */
  94.  
  95.             UnLock (lock);
  96.         } else if (IoErr() == ERROR_OBJECT_IN_USE) {
  97.             retval = RETURN_OK;
  98.             ostr = (argv->name || argv->full) ? argv->file : "1";
  99.         } else {
  100.             ostr = (argv->name || argv->full) ? "" : "0";
  101.         } /* if */
  102. #else
  103.  
  104.         struct AnchorPath  *ap;
  105.         UBYTE  apb[sizeof (*ap) + 8 + MATHPATHLEN];
  106.         ap          = (void *)(((ULONG)apb + 7) & ~7);
  107.         ap->ap_Strlen      = MAXPATHLEN;
  108.         ap->ap_BreakBits  = 0;
  109.         ap->ap_FoundBreak = 0;
  110.         ap->ap_Flags      = 0;
  111.         error = MatchFirst(argv->file, ap);
  112.         MathEnd (ap);
  113.         if (!error) {
  114.             retval = RETURN_OK;
  115.             ostr = (argv->full) ? ap->ap_Buf :
  116.                      (argv->name) ? argv->file : "1";
  117.         } else {
  118.             ostr = (argv->name || argv->full) ? "" : "0";
  119.         } /* if */
  120.  
  121. #endif
  122.  
  123.         /* ----- Result output, either boolean(0/1) or named (name/"") */
  124.         PutStr( ostr );
  125.         PutStr( "\n" );
  126.         }
  127.  
  128.         if (p != NULL) {
  129.         p->pr_WindowPtr = pw;
  130.         } /* if */
  131.  
  132.     }};
  133. };
  134.  
  135. #endif
  136.  
  137.  
  138.